package com.wlc.exception.demo01;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description 测试异常
 * @date 2021-07-08 13:51
 */
public class TestException {

    private String name;

    public void method01() {
        File file = new File("/Users/wanglichao/Desktop/mysql  创建用户");
        // 读取file文件。
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            System.out.println("文件找不到异常");
        }
    }

    public int method02() {
        try {
            throw new NullPointerException("空指针异常");
        } catch (Exception e) {
            return 2;
        } finally {
            return 3;
        }
    }

    public static int method03() {
        try {
            return return1();
        } catch (Exception e) {
            return return2();
        } finally {
            return return3();
        }
    }

    private static int return1() {
        System.out.println("return 1");
        return 1;
    }

    private static int return2() {
        System.out.println("return 2");
        return 2;
    }

    private static int return3() {
        System.out.println("return 3");
        return 3;
    }

    public void method04() {
        File f = new File("/Users/wanglichao/Desktop/笔记/redis相关/redis笔记.rtf");

        try {
            System.out.println("试图打开 /Users/wanglichao/Desktop/笔记/redis相关/redis笔记.rtf");
            new FileInputStream(f);
            System.out.println("成功打开");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = sdf.parse("2016/06/03");
        } catch (FileNotFoundException e) {
            System.out.println("/Users/wanglichao/Desktop/笔记/redis相关/redis笔记.rtf");
            e.printStackTrace();
        } catch (ParseException e) {
            System.out.println("日期格式解析错误");
            e.printStackTrace();
        }
    }

    public void method05() {
       String a = null;
       try {
           a.length();
       }catch (Throwable e){
           System.out.println("空指针异常");
       }
    }


    public static void main(String[] args) {
        TestException testException = new TestException();
        testException.method05();
    }
}
